package com.bdmc.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.car.domain.CarMaintenanceDetail;
import com.bdmc.car.domain.bo.CarMaintenanceDetailBo;
import com.bdmc.car.domain.vo.CarMaintenanceDetailVo;
import com.bdmc.car.mapper.CarMaintenanceDetailMapper;
import com.bdmc.car.service.ICarMaintenanceDetailService;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 维修详单Service业务层处理
 *
 * @author bdmc
 * @date 2023-01-06
 */
@Service
public class CarMaintenanceDetailServiceImpl extends ServicePlusImpl<CarMaintenanceDetailMapper, CarMaintenanceDetail, CarMaintenanceDetailVo> implements ICarMaintenanceDetailService {

    @Override
    public CarMaintenanceDetailVo queryById(Integer detailId){
        return getVoById(detailId);
    }

    @Override
    public TableDataInfo<CarMaintenanceDetailVo> queryPageList(CarMaintenanceDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CarMaintenanceDetail> lqw = buildQueryWrapper(bo);
        Page<CarMaintenanceDetailVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<CarMaintenanceDetailVo> mySelectPage(CarMaintenanceDetailBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }


    @Override
    public List<CarMaintenanceDetailVo> queryList(CarMaintenanceDetailBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<CarMaintenanceDetail> buildQueryWrapper(CarMaintenanceDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CarMaintenanceDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMaintenanceId() != null, CarMaintenanceDetail::getMaintenanceId, bo.getMaintenanceId());
        lqw.eq(bo.getItemId() != null, CarMaintenanceDetail::getItemId, bo.getItemId());
        lqw.eq(bo.getItemCount() != null, CarMaintenanceDetail::getItemCount, bo.getItemCount());
        lqw.eq(bo.getAmount() != null, CarMaintenanceDetail::getAmount, bo.getAmount());
        return lqw;
    }

    @Override
    public Boolean insertByBo(CarMaintenanceDetailBo bo) {
        CarMaintenanceDetail add = BeanUtil.toBean(bo, CarMaintenanceDetail.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(CarMaintenanceDetailBo bo) {
        CarMaintenanceDetail update = BeanUtil.toBean(bo, CarMaintenanceDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CarMaintenanceDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
